/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gestionedt.modules;

import gestionedt.controllers.EnseignantJpaController;
import gestionedt.controllers.GroupeJpaController;
import gestionedt.controllers.ModuleJpaController;
import gestionedt.controllers.PoolJpaController;
import gestionedt.controllers.PrestationJpaController;
import gestionedt.models.Groupe;
import gestionedt.models.Module;
import gestionedt.models.Pool;
import gestionedt.models.Prestation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessages;

/**
 * 
 */
public class ModulesAction extends DispatchAction {
    
    
    /**
     * Liste tous les modules.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward lister(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        ModulesForm mForm = (ModulesForm) form;
        ModuleJpaController ct = new ModuleJpaController();
        mForm.setModules(ct.getList());
        
        return mapping.findForward("liste");
    }

    /**
     * Affiche le formulaire d'ajout d'un nv module.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward afficherNv(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        return mapping.findForward("formModule");
    }

    /**
     * Affiche le formulaire de modification d'un module existant.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward afficher(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ModulesForm mForm = (ModulesForm) form;
        int idModule = mForm.getIdModule();
        ModuleJpaController ctModule = new ModuleJpaController();
        Module module = ctModule.findModuleAvecRel(idModule);
        mForm.setModule(module);

        switch(mForm.getSousForm()) {
            case GROUPES:
                GroupeJpaController ctGroupe = new GroupeJpaController();
                mForm.setGroupes(ctGroupe.getList());
                break;
            case POOLS:
                PoolJpaController ctPool = new PoolJpaController();
                mForm.setPools(ctPool.getList());
                String[] idsPoolsSel = new String[module.getPools().size()];
                int i = 0;
                for(Pool p : module.getPools())
                    idsPoolsSel[i++] = p.getId().toString();
                mForm.setIdsPoolsSelectionnes(idsPoolsSel);
                break;
            case PRESTATIONS:
            default:
                PrestationJpaController ctPrestation = new PrestationJpaController();
                mForm.setPrestations(ctPrestation.findPrestationsParModule(module));
                EnseignantJpaController ctEnseignant = new EnseignantJpaController();
                mForm.setEnseignants(ctEnseignant.getList());
        }

        return mapping.findForward("formModule");
    }

    /**
     * Supprime un module.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward supprimer(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ModulesForm mForm = (ModulesForm) form;
        int idModule = mForm.getIdModule();
        ModuleJpaController ctModule = new ModuleJpaController();
        ctModule.destroy(idModule);

        return mapping.findForward("lister");
    }

    /**
     * Ajoute une prestation pour le module concerné.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward ajouterPrestation(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ModulesForm mForm = (ModulesForm) form;
        ActionMessages erreurs = mForm.validatePrestation(mapping, request);
        if( ! erreurs.isEmpty()) {
            this.saveMessages(request, erreurs);
            return afficher(mapping, form, request, response);
        }

        ModuleJpaController ctModule = new ModuleJpaController();
        EnseignantJpaController ctEns = new EnseignantJpaController();

        // instanciation de la nouvelle prestation
        Prestation nvPrestation = new Prestation();
        int idEnseignant = mForm.getIdIntEnsSel();
        nvPrestation.setEnseignant(ctEns.findEnseignant(idEnseignant));
        // ajout de la nouvelle prestation au module concerné
        Module module = ctModule.findModuleAvecRel(mForm.getIdModule());
        nvPrestation.setModule(module);
        module.getPrestations().add(nvPrestation);
        ctModule.edit(module);

        return afficher(mapping, form, request, response);
    }

    /**
     * Supprime une prestation pour le module concerné.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward supprimerPrestation(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ModulesForm mForm = (ModulesForm) form;
        ModuleJpaController ctModule = new ModuleJpaController();
        PrestationJpaController ctPrest = new PrestationJpaController();
        Prestation p = ctPrest.findPrestation(mForm.getIdPrestation());
        Module m = p.getModule();
        m.getPrestations().remove(p);
        ctPrest.destroy(mForm.getIdPrestation());
        ctModule.edit(m);

        return afficher(mapping, form, request, response);
    }

    /**
     * Inscrit un groupe au module concerné.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward inscrireGrp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ModulesForm mForm = (ModulesForm) form;
        ActionMessages erreurs = mForm.validateInscripGroupe(mapping, request);
        if( ! erreurs.isEmpty()) {
            this.saveMessages(request, erreurs);
            return afficher(mapping, form, request, response);
        }

        ModuleJpaController ctModule = new ModuleJpaController();
        GroupeJpaController ctGroupe = new GroupeJpaController();

        Module module = ctModule.findModule(mForm.getIdModule());
        Groupe groupe = ctGroupe.findGroupe(mForm.getIdIntGrpSel());

        // si le groupe n'est pas déjà inscrit :
        if( ! module.getGroupesInscrits().contains(groupe)) {
            module.getGroupesInscrits().add(groupe);
            ctModule.edit(module);
        }
        
        return afficher(mapping, form, request, response);
    }

    /**
     * Désinscrit un groupe du module concerné.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward desinscrireGrp(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ModulesForm mForm = (ModulesForm) form;

        ModuleJpaController ctModule = new ModuleJpaController();
        GroupeJpaController ctGroupe = new GroupeJpaController();

        Module module = ctModule.findModule(mForm.getIdModule());
        Groupe groupe = ctGroupe.findGroupe(mForm.getIdGroupe());

        module.getGroupesInscrits().remove(groupe);
        ctModule.edit(module);

        return afficher(mapping, form, request, response);
    }

    /**
     * Associe les pools sélectionnés au module concerné.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws java.lang.Exception
     */
    public ActionForward modifierPools(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        ModulesForm mForm = (ModulesForm) form;

        ModuleJpaController ctModule = new ModuleJpaController();
        PoolJpaController ctPool = new PoolJpaController();

        Module module = ctModule.findModule(mForm.getIdModule());
        module.getPools().clear();
        module.setPools(ctPool.findPoolsByIds(mForm.getIdsIntPoolsSelectionnes()));
        ctModule.edit(module);

        return afficher(mapping, form, request, response);
    }

}